/*
2010/9/7 18:47:17
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using ExClassLib;

namespace LanSunSystem
{
    public partial class GoodsMasterProperty : BaseDockForm
    {
        DataRow dwGoodsMasterProperty = null;
        ToolStripMenuItem sender = new ToolStripMenuItem();
        string Reason="";
        public override void ProcessWFMessage(MessageEx This_Message)
        {
            try
            {
                base.ProcessWFMessage(This_Message);
                string JobID = This_Message.JobID;
                string JobType = This_Message.JobType;
                string JobContent = This_Message.JobContent;
                string JobID_Req = This_Message.JobID_Req;
                switch (JobID)
                {
                    case "OnCheckGeneral":
                        this.CheckGeneral(This_Message);
                        break;
	               case "OnCheckGMKRFK010_GoodsMasterProperty_MainKey":
                        this.CheckGMKRFK010_GoodsMasterProperty_MainKey(This_Message);
                        break;
	               case "OnCheckGMKRFK010_GoodsMasterData_MainKey":
                        this.CheckGMKRFK010_GoodsMasterData_MainKey(This_Message);
                        break;
	                case "OnThisFail":
                       foreach (DataTable dtTemp in This_Message.dsRequest.Tables)
                        {
                            if (!dtTemp.TableName.Contains("QueryCond")) 
                                continue;
                            else 
                                Reason = dtTemp.Rows[0]["Reason1"].ToString().Trim();  
                        }
                        switch (Reason)
                        {
	               case "GMKRFK010_GoodsMasterProperty_MainKey":
                        UpdateByGMKRFK010_GoodsMasterProperty_MainKey(This_Message);
                        break;
	               case "GMKRFK010_GoodsMasterData_MainKey":
                        UpdateByGMKRFK010_GoodsMasterData_MainKey(This_Message);
                        break;
                   }
                    break;                        
                    default:
                        break;
                  
                }
            }            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.Source + ex.StackTrace);
            }
        }
          
        #region CheckGMKRFK010_GoodsMasterProperty_MainKey
       private void CheckGMKRFK010_GoodsMasterProperty_MainKey(MessageEx MsgReturn)
        {
            try
            {
                DataRow dwThis = m_Base.dwCurrentRecord;
                Reason="GMKRFK010_GoodsMasterProperty_MainKey";     
                if (!CheckRelation(dwThis, "209"))
                {
                    sender.Name = "OnThisPass";
                    this.m_Base.SubMenu_Click(sender, null);
                    return;
                }                 
                #region KeyInstanceTable
                #endregion
                string basetable = "";
                string QueryType="QueryOnly";
                string QueryCond="";
                DataRow dwBaseQuery = null;
                DataSet DataSetEvent = new DataSet();
                Queue PrimaryKeyList = new Queue();
                Queue ForeignKeyList = new Queue();
       
		            string SysID=dwThis["SysID"].ToString().Trim();
                    ForeignKeyList.Enqueue("SysID");                    
		            string KeyDefType=dwThis["KeyDefType"].ToString().Trim();
                    ForeignKeyList.Enqueue("KeyDefType");                    
		            string GoodsTypeKey=dwThis["GoodsTypeKey"].ToString().Trim();
                    ForeignKeyList.Enqueue("GoodsTypeKey");                    
		            string GoodsTypeKeyDefKey=dwThis["GoodsTypeKeyDefKey"].ToString().Trim();
                    ForeignKeyList.Enqueue("GoodsTypeKeyDefKey");                    
		            string GoodsKey=dwThis["GoodsKey"].ToString().Trim();
                    ForeignKeyList.Enqueue("GoodsKey");                    
                        PrimaryKeyList.Enqueue("SysID");                        
                        PrimaryKeyList.Enqueue("KeyDefType");                        
                        PrimaryKeyList.Enqueue("GoodsTypeKey");                        
                        PrimaryKeyList.Enqueue("GoodsTypeKeyDefKey");                        
                        PrimaryKeyList.Enqueue("GoodsKey");                        
                if (SysID==""||KeyDefType==""||GoodsTypeKey==""||GoodsTypeKeyDefKey==""||GoodsKey=="")
                {
                    sender.Name = "OnThisFail";
                    basetable = "GoodsMasterKeyQueryCond";
                    string KeysTable = "";
                    QueryCond="SysID";
                  if (basetable == "KeyInstanceQueryCond")
                    {
                     
                        if(dwThis.Table.Columns.Contains("KeyInstanceTable"))
                        {
                            KeysTable= dwThis["KeyInstanceTable"].ToString().Trim();
                        }
                         if(dwThis.Table.Columns.Contains("SKeyInstanceTable"))
                        { 
                             KeysTable= dwThis["SKeyInstanceTable"].ToString().Trim();
                        }
                        if(KeysTable!="")
                          {
                                basetable = KeysTable + "QueryCond";
                                QueryCond="IX_"+KeysTable;
                          }
                    }
                    dwBaseQuery = m_Base.MapDataRow(QueryType, QueryCond, basetable, m_Base.dwCurrentRecord,ForeignKeyList,PrimaryKeyList); 
                    dwBaseQuery["Reason1"]=Reason;
                    DataSetEvent = this.m_Base.ImportDataTable(DataSetEvent, dwBaseQuery);
                    this.m_Base.RequestMessage.dsRequest = DataSetEvent;
                }
                else
                {
                    sender.Name = "OnThisPass";
                }
                this.m_Base.SubMenu_Click(sender, null);
            }
            catch (System.Exception ex)
            {
                m_log.Error(ex);
                MessageBox.Show(ex.Message + ex.Source + ex.StackTrace + ex.TargetSite);
            }
        }
	          
           #endregion

        #region  UpdateByGMKRFK010_GoodsMasterProperty_MainKey
        private void UpdateByGMKRFK010_GoodsMasterProperty_MainKey(MessageEx MsgReturn)
        {
           try
            {    
               DataRow dwThis = m_Base.dwCurrentRecord;
           #region FPrimaryKeyTable==SourceTable
                 
                string basetable = "GoodsMasterKey"; 
                Queue PrimaryKeyList = new Queue();
                Queue ForeignKeyList = new Queue();
                    ForeignKeyList.Enqueue("SysID");                    
                    ForeignKeyList.Enqueue("KeyDefType");                    
                    ForeignKeyList.Enqueue("GoodsTypeKey");                    
                    ForeignKeyList.Enqueue("GoodsTypeKeyDefKey");                    
                    ForeignKeyList.Enqueue("GoodsKey");                    
                        PrimaryKeyList.Enqueue("SysID");                        
                        PrimaryKeyList.Enqueue("KeyDefType");                        
                        PrimaryKeyList.Enqueue("GoodsTypeKey");                        
                        PrimaryKeyList.Enqueue("GoodsTypeKeyDefKey");                        
                        PrimaryKeyList.Enqueue("GoodsKey");                        
                DataRow dwSource = MsgReturn.dsRequest.Tables[basetable].Rows[0];
                m_Base.dwCurrentRecord = m_Base.UpdateDataRow(m_Base.dwCurrentRecord, dwSource,ForeignKeyList,PrimaryKeyList);
                m_Base.dwCurrentRecord.AcceptChanges();
                    foreach (DataTable dtTemp in MsgReturn.dsRequest.Tables)
                    {
                        if (dtTemp.TableName != this.m_Base.BaseTable)
                        {
                            m_Base.BaseDataSet = m_Base.ImportDataTable(m_Base.BaseDataSet, dtTemp);
                        }
                    }   
                sender.Name = "OnUpdateDone";
                this.m_Base.SubMenu_Click(sender, null);
            #endregion
            
            }
            catch (System.Exception ex)
            {
                m_log.Error(ex);
                MessageBox.Show(ex.Message + ex.Source + ex.StackTrace + ex.TargetSite);
            }
       }
#endregion
        
        #region CheckGMKRFK010_GoodsMasterData_MainKey
       private void CheckGMKRFK010_GoodsMasterData_MainKey(MessageEx MsgReturn)
        {
            try
            {
                DataRow dwThis = m_Base.dwCurrentRecord;
                Reason="GMKRFK010_GoodsMasterData_MainKey";     
                if (!CheckRelation(dwThis, "211"))
                {
                    sender.Name = "OnThisPass";
                    this.m_Base.SubMenu_Click(sender, null);
                    return;
                }                 
                #region KeyInstanceTable
                if (!CheckKeyInstanceTable(dwThis, Reason))
                {
                    sender.Name = "OnThisPass";
                    this.m_Base.SubMenu_Click(sender, null);
                    return;
                }    
                #endregion
                string basetable = "";
                string QueryType="QueryOnly";
                string QueryCond="";
                DataRow dwBaseQuery = null;
                DataSet DataSetEvent = new DataSet();
                Queue PrimaryKeyList = new Queue();
                Queue ForeignKeyList = new Queue();
       
		            string SysID=dwThis["SysID"].ToString().Trim();
                    ForeignKeyList.Enqueue("SysID");                    
		            string KeyDefType=dwThis["KeyDefType"].ToString().Trim();
                    ForeignKeyList.Enqueue("KeyDefType");                    
		            string GoodsTypeKey=dwThis["GoodsTypeKey"].ToString().Trim();
                    ForeignKeyList.Enqueue("GoodsTypeKey");                    
		            string GoodsTypeKeyDefKey=dwThis["GoodsTypeKeyDefKey"].ToString().Trim();
                    ForeignKeyList.Enqueue("GoodsTypeKeyDefKey");                    
		            string GoodsKey=dwThis["GoodsKey"].ToString().Trim();
                    ForeignKeyList.Enqueue("GoodsKey");                    
		            string PositionID=dwThis["PositionID"].ToString().Trim();
                    ForeignKeyList.Enqueue("PositionID");                    
                        PrimaryKeyList.Enqueue("SysID");                        
                        PrimaryKeyList.Enqueue("KeyDefType");                        
                        PrimaryKeyList.Enqueue("GoodsTypeKey");                        
                        PrimaryKeyList.Enqueue("GoodsTypeKeyDefKey");                        
                        PrimaryKeyList.Enqueue("GoodsKey");                        
                        PrimaryKeyList.Enqueue("PositionID");                        
                        string SubStatus = dwThis["SubStatus"].ToString().Trim();
                        QueryType="CreateByQuery";
                if (!SubStatus.Contains("KeyInstanceComplete"))
                {
                    sender.Name = "OnThisFail";
                    basetable = "KeyInstanceQueryCond";
                    string KeysTable = "";
                    QueryCond="IX_GoodsMasterData_MainKey";
                  if (basetable == "KeyInstanceQueryCond")
                    {
                     
                        if(dwThis.Table.Columns.Contains("KeyInstanceTable"))
                        {
                            KeysTable= dwThis["KeyInstanceTable"].ToString().Trim();
                        }
                         if(dwThis.Table.Columns.Contains("SKeyInstanceTable"))
                        { 
                             KeysTable= dwThis["SKeyInstanceTable"].ToString().Trim();
                        }
                        if(KeysTable!="")
                          {
                                basetable = KeysTable + "QueryCond";
                                QueryCond="IX_"+KeysTable;
                          }
                    }
                    dwBaseQuery = m_Base.MapDataRow(QueryType, QueryCond, basetable, m_Base.dwCurrentRecord,ForeignKeyList,PrimaryKeyList); 
                    dwBaseQuery["Reason1"]=Reason;
                    DataSetEvent = this.m_Base.ImportDataTable(DataSetEvent, dwBaseQuery);
                    this.m_Base.RequestMessage.dsRequest = DataSetEvent;
                }
                else
                {
                    sender.Name = "OnThisPass";
                }
                this.m_Base.SubMenu_Click(sender, null);
            }
            catch (System.Exception ex)
            {
                m_log.Error(ex);
                MessageBox.Show(ex.Message + ex.Source + ex.StackTrace + ex.TargetSite);
            }
        }
	          
           #endregion

        #region  UpdateByGMKRFK010_GoodsMasterData_MainKey
        private void UpdateByGMKRFK010_GoodsMasterData_MainKey(MessageEx MsgReturn)
        {
           try
            {    
               DataRow dwThis = m_Base.dwCurrentRecord;
           #region FPrimaryKeyTable==SourceTable
            #endregion
            #region FForeignKeyTable==SourceTable
            string TargetKeyTable="";
            TargetKeyTable= "KeyInstance";
            if(TargetKeyTable=="KeyInstance")
            {
                TargetKeyTable=dwThis["KeyInstanceTable"].ToString().Trim();
            }
            DataTable dtDataSetConclusion = MsgReturn.dsRequest.Tables["DataSetConclusion"];
                var query = from dwConclusion in dtDataSetConclusion.AsEnumerable()
                            where dwConclusion["TableName"].ToString().TrimEnd() ==TargetKeyTable
                            select dwConclusion;
                string Conclusion = "";
                if (query.Count() > 0)
                {
                    foreach (DataRow dwTemp in query)
                    {
                        Conclusion = dwTemp["Conclusion"].ToString().Trim();
                    }
                }
                if (Conclusion == "TOTALCOMPLETE")
                {
                    m_Base.dwCurrentRecord["SubStatus"] +=  "KeyInstanceComplete";
                    foreach (DataTable dtTemp in MsgReturn.dsRequest.Tables)
                    {
                        if (dtTemp.TableName != this.m_Base.BaseTable)
                        {
                            m_Base.BaseDataSet = m_Base.ImportDataTable(m_Base.BaseDataSet, dtTemp);
                        }
                    }

                    
                }
                else
                {
                }
                sender.Name = "OnUpdateDone";
                this.m_Base.SubMenu_Click(sender, null);
            #endregion
            
            }
            catch (System.Exception ex)
            {
                m_log.Error(ex);
                MessageBox.Show(ex.Message + ex.Source + ex.StackTrace + ex.TargetSite);
            }
       }
#endregion
        
      
        private void CheckGeneral(MessageEx MsgReturn)
        {
            try
            {
                dwGoodsMasterProperty = m_Base.dwCurrentRecord;
                string LongDescription = dwGoodsMasterProperty["LongDes"].ToString().Trim();
                string InstanceID = dwGoodsMasterProperty["InstanceID"].ToString().Trim();
                
                object m_QueryCond = null;
                string basetable = "";
                DataRow dwBaseQuery = null;
                DataSet DataSetEvent = new DataSet();
                if (LongDescription == "")
                {
                    sender.Name = "OnThisFail";
                    this.m_Base.SubMenu_Click(sender, null);
                }
                else
                {
                    if (InstanceID == "")
                    {
                        sender.Name = "OnThisFail";
                        this.m_Base.SubMenu_Click(sender, null);
                        System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));
                        sender.Name = "OnCreateByQuery";
                        this.m_Base.SubMenu_Click(sender, null);
                    }
                    else
                    {
                        dwGoodsMasterProperty["SubStatus"] = "Complete";
                        dwGoodsMasterProperty["Status"] = "Complete";
                        dwGoodsMasterProperty.AcceptChanges();
                        this.m_Base.mnu_FileSaveItem.PerformClick();
                        sender.Name = "OnThisPass";
                        this.m_Base.SubMenu_Click(sender, null);
                    }

                }
            }
            catch (System.Exception ex)
            {
                m_log.Error(ex);
                MessageBox.Show(ex.Message + ex.StackTrace + ex.TargetSite);
            }
        }
  
        #region Windows Form Designer generated code
        public ExGoodsMasterProperty m_Base;
 
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
         
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // GoodsMasterProperty
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(1020, 724);
            this.Font = new System.Drawing.Font("SimSun", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.Name = "GoodsMasterProperty";
            this.Text = "GoodsMasterProperty";
            this.ResumeLayout(false);

        }
        
        private void mnu_FileExitItem_Click(object sender, EventArgs e)
        {
            DataSet dsreturn = new DataSet();
            if (RequestMessage.NeedReturn)
            {
                dsreturn = m_Base.ImportDataTable(dsreturn, m_Base.dwCurrentRecord);
            }
            else
            {
                dsreturn = null;
            }
            ReplyReqJob("Return", m_Base.RequestMessage, dsreturn);
            this.Close();
        }
 
        
  
        public GoodsMasterProperty()
        {
            InitializeComponent();
            m_AgentCallbackPath = this.Name + Guid.NewGuid().ToString();
            m_Base = new ExGoodsMasterProperty();
             LinkEvent();
        }
        private void LinkEvent()
        {
            this.Load += new EventHandler(Control_Load);
            this.m_Base.mnu_FileExitItem.Click += new EventHandler(mnu_FileExitItem_Click);
       }
        private void Control_Load(object sender, EventArgs e)
        {

            this.dicBaseInitPara = dicBaseInitPara;
            PapaAppID = (string)dicBaseInitPara["AppID"];
            dicBaseInitPara["AppID"] = this.Name;
            ExProfileLoad("");
        }
        private void ExProfileLoad(string strProfileID)
        {
            try
            {
                this.m_Base.dicBaseInitPara = dicBaseInitPara;
                if (strProfileID != "")
                    m_Base.dicBaseInitPara["ProfileID"] = strProfileID;
                else
                    m_Base.dicBaseInitPara["ProfileID"] = "1000";

                this.m_Base.dicBaseInitPara["FormType"] = "Default";
                this.m_Base.Location = new System.Drawing.Point(3, 3);

                this.m_Base.TabIndex = 0;
                this.m_tableLayoutPanel.Controls.Add(this.m_Base, 0, 0);
                this.m_tableLayoutPanel.SetColumnSpan(this.m_Base, 2);
                this.m_tableLayoutPanel.SetRowSpan(this.m_Base, 2);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.Source + ex.StackTrace);
            }

        }    
        
     }
      #endregion
}
namespace LanSunSystem
{
  public partial class GoodsMasterPropertyQueryCond : BaseDockForm
    {
        public ExGoodsMasterPropertyQueryCond m_Base;
        #region FixedEvent
        public override void ProcessWFMessage(MessageEx This_Message)
        {
            try
            {
                base.ProcessWFMessage(This_Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.Source + ex.StackTrace);
            }

        }
        private void LinkEvent()
        {
            this.Load += new System.EventHandler(this.Control_Load);
            this.m_Base.mnu_FileQueryItem.Click += new EventHandler(mnu_FileQueryItem_Click);
        }
        private void mnu_FileQueryItem_Click(object sender, EventArgs e)
        {
            try
            {
                ReplyReqJob("Return", m_Base.RequestMessage, null);
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.Source + ex.StackTrace);
            }
        }

        private void Control_Load(object sender, EventArgs e)
        {

            this.dicBaseInitPara = dicBaseInitPara;
            PapaAppID = (string)dicBaseInitPara["AppID"];
            dicBaseInitPara["AppID"] = this.Name;
            ExProfileLoad("");
        }
        private void ExProfileLoad(string strProfileID)
        {
            m_Base.dicBaseInitPara = dicBaseInitPara;
            m_Base.dicBaseInitPara["ProfileID"] = "1000";

            m_Base.Location = new System.Drawing.Point(3, 3);
            m_Base.Size = new System.Drawing.Size(1024, 720);
            m_Base.TabIndex = 0;
            this.m_tableLayoutPanel.Controls.Add(m_Base, 0, 0);
            this.m_tableLayoutPanel.SetColumnSpan(m_Base, 2);
            this.m_tableLayoutPanel.SetRowSpan(m_Base, 2);
        }
        #endregion
        public GoodsMasterPropertyQueryCond()
        {
            try
            {
                InitializeComponent();
                m_AgentCallbackPath = this.Name + Guid.NewGuid().ToString();
                m_Base = new ExGoodsMasterPropertyQueryCond();
                LinkEvent();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.Source + ex.StackTrace);
            }
        }
        #region Windows Form Designer generated code
 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // GoodsMasterPropertyQueryCond
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.Color.PaleGreen;
            this.ClientSize = new System.Drawing.Size(1024, 726);
            this.Font = new System.Drawing.Font("SimSun", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.Name = "GoodsMasterPropertyQueryCond";
            this.Text = "GoodsMasterPropertyQueryCond";
            this.ResumeLayout(false);

        }
        #endregion
    }
}
